package com.whut.whw.util;

import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 张芳容 on 2017/06/23.
 * 输入的相关工具类
 */

public final class InputUtils {
    /**
     * 禁止EditText输入空格
     * @param editText
     */
    public static void setEditTextInhibitInputSpace(EditText editText){
        InputFilter filter=new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if(source.equals(" ")) return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }

    /**
     * 禁止EditText输入特殊字符
     * @param editText
     */
    public static void setEditTextInhibitInputSpeChat(EditText editText){

        InputFilter filter=new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String speChat="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
                Pattern pattern = Pattern.compile(speChat);
                Matcher matcher = pattern.matcher(source.toString());
                if(matcher.find())return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }
    /**
     * 设置只能输入浮点数
     * @param editText
     */
    public static void setEditTextFloat(EditText editText,int before,int back){

        editText.setInputType(InputType.TYPE_CLASS_NUMBER| InputType.TYPE_NUMBER_FLAG_DECIMAL);
        editText.setFilters(new InputFilter[] {new DecimalInputFilter(before, back)});
    }

    /**
     * 设置输入的长度限制
     * @param editText
     * @param length
     */
    public static void setEditTextMaxLength(EditText editText,int length){
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
    }

    private static class DecimalInputFilter implements InputFilter {
        private String mRegularExpression;

        public DecimalInputFilter(int firstLength, int lastLength) {
            mRegularExpression = String.format("(\\d{0,%d}(\\.\\d{0,%d})?)", firstLength, lastLength);
        }

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

            boolean delete = false;
            StringBuilder builder = new StringBuilder(dest);

            if (TextUtils.isEmpty(source)) {
                delete = true;
                builder.delete(dstart, dend);
            } else {
                builder.insert(dstart, source);
            }

            String value = builder.toString();

            return value.matches(mRegularExpression) ? null : delete ? "." : "";
        }
    }

}
